#include "messageHandler.h"
#include<json-c/json.h>
#include"stdShared.h"
#include"stdTcpService.h"
/*构造函数*/
MessageHandle::MessageHandle(void *arg ):m_musicServer(arg)
{
   
    m_hanleds[REGISTER]=[this](const std::string&data){m_musicServer.handleRegister(data);};
    m_hanleds[LOGIN]=[this](const std::string&data){m_musicServer.handleLoginInfo(data);};
    // m_hanleds[SEARCH_MUSIC]=
    // m_hanleds[DOWNLOAD_MUSIC]=
}
void MessageHandle::hanldeMessage(const std::string &data)
{
    json_object* jsonObj=json_tokener_parse(data.c_str());
    if(jsonObj==NULL)
    {
        std::cout<<"解析失败"<<std::endl;
        return;
    }
    /*json字符串解析成功*/
    int type=json_object_get_int(json_object_object_get(jsonObj,"type"));
    
    auto iter=m_hanleds.find(type);
    if(iter!=m_hanleds.end())
    {
        /*执行回调函数*/
        iter->second(data);
    }
    else
    {
        /*处理位置的消息类型*/
        std::cout<<"unknow message type"<<type<<std::endl;
    }
}